//
//  RightViewController.m
//  PCNews
//
//  Created by PengchengWang on 16/3/1.
//  Copyright © 2016年 pengchengWang. All rights reserved.
//

#import "RightViewController.h"
#import "MenuTableViewCell.h"
#import "FavoriteViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import <AVOSCloud.h>
#import "SetViewController.h"

@interface RightViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)UIImageView *imgUserIcon;
@property(nonatomic,strong)UILabel *lblUserName;

@end

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    // Do any additional setup after loading the view.
}

-(void)setViews{
    UIView * topView =[[UIView alloc]initWithFrame:CGRectMake(kScreenWidth - 200, 0, 200, 64)];
    self.imgUserIcon =[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 40, 40)];
    self.lblUserName =[[UILabel alloc]initWithFrame:CGRectMake(70, 20, 250, 40)];
    self.lblUserName.font=[UIFont fontWithName:@"Helvetica" size:20];
    self.lblUserName.textColor =[UIColor whiteColor];
    [self setTopView];
    topView.backgroundColor=kColor(48, 48, 48, 1);
    [topView addSubview:self.imgUserIcon];
    [topView addSubview:self.lblUserName];
    UITapGestureRecognizer *tapTopView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTopViewAction)];
    [topView addGestureRecognizer:tapTopView];
    [self.view addSubview:topView];
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(kScreenWidth - 200, 64, 200, kScreenHeight-74) style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableV];
    self.tableV.backgroundColor = kColor(60, 60, 60, 1);
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
#pragma mark - UITableView 行线隐藏 separatorStyle
    self.tableV.separatorStyle = NO;
    [self.mbPreHUD hide:YES];
    UIView * footView =[[UIView alloc]initWithFrame:CGRectMake(kScreenWidth - 200, kScreenHeight-30, 200, 30)];
    UILabel *footLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 0, 80, 30)];
    footLabel.text = kVersionNumber;
    footLabel.font = [UIFont systemFontOfSize:13];
    footLabel.textColor = [UIColor whiteColor];
    footView.backgroundColor = kColor(48, 48, 48, 1);
    [footView addSubview:footLabel];
    [self.view addSubview:footView];
    [self.tableV registerNib:[UINib nibWithNibName:@"MenuTableViewCell" bundle:nil] forCellReuseIdentifier:@"MenuTableViewCell"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(observerAction:) name:@"openRightV" object:nil];
}

-(void)observerAction:(NSNotification*)sender{
    [self setTopView];
}

-(void)setTopView{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        self.lblUserName.text = currentUser.username;
        AVQuery *query = [AVQuery queryWithClassName:@"UserIcon"];
        //设置请求查询条件
        [query whereKey:@"username" equalTo:self.lblUserName.text];
        //找到符合条件的数据
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (objects.count) {
                NSDictionary *dic = objects[0];
                //创建AVFile对象接受网络中下载的数据
                AVFile *imgFil = dic[@"userIcon"];
                //将AVFile数据转换为NSData类型
                NSData *imgData = [imgFil getData];
                //将NSData转换为UIImage使用
                self.imgUserIcon.image = [UIImage imageWithData:imgData];
            } else {
                // 输出错误信息
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
        }];
    } else {
        self.imgUserIcon.image=[UIImage imageNamed:@"account_default_icon"];
        self.lblUserName.text =@"账号登录";
    }
}

-(void)tapTopViewAction{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"用户注销" message:@"您确定要注销吗?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionLogout = [UIAlertAction actionWithTitle:@"朕去意义决" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [AVUser logOut];
            [self setViews];
            [self setTopView];
        }];
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"算了" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:actionLogout];
        [alert addAction:actionCancel];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        [DrawerViewController setRootViewController:[LoginViewController class]];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
};

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuTableViewCell" forIndexPath:indexPath];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor =kColor(79, 79, 79, 1);
    cell.backgroundColor =kColor(60, 60, 60, 1);
    cell.headCell.textColor=[UIColor whiteColor];
    if (indexPath.row ==0) {
        cell.imgV.image =[UIImage imageNamed:@"menu_right_collect"];
        cell.headCell.text=@"我的收藏";
    }else if (indexPath.row == 1){
        cell.imgV.image =[UIImage imageNamed:@"menu_right_setting"];
        cell.headCell.text=@"设置";
    }else if (indexPath.row == 2){
        cell.imgV.image =[UIImage imageNamed:@"menu_right_theme_day"];
        cell.headCell.text=@"夜间模式";
    }else if (indexPath.row == 3){
        cell.imgV.image =[UIImage imageNamed:@"menu_right_search"];
        cell.headCell.text=@"新闻搜索";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        [DrawerViewController setRootViewController:[FavoriteViewController class]];
    }else if (indexPath.row == 1){
        [DrawerViewController setRootViewController:[SetViewController class]];
        
        
    }else if (indexPath.row == 2){
        UIWindow *delgt = [UIApplication sharedApplication].keyWindow;
        delgt.alpha = .4;
        
        
    }else if (indexPath.row == 3){
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
